package net.wantongtech.xa.uploadbasicinfo.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
import com.google.common.io.Files;
import com.google.common.io.LineProcessor;
import com.wtkj.vo.dzzf.base.kfwd.CardInfo;
import com.wtkj.vo.dzzf.share.staticvalue.CardStaticValue;
import com.wtkj.zwxt.enums.SignSourceEnum;
import lombok.extern.slf4j.Slf4j;
import net.wantongtech.xa.uploadbasicinfo.dao.CommonBaseDao;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.LocalDate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class ErrorUserTypeService {


    @Resource
    private CommonBaseDao commonBaseDao;

    private final Map<String, Integer> countMap = new HashMap<>();

    private final List<String> errorCardList = Lists.newArrayList();

    private final ObjectMapper objectMapper = new ObjectMapper();


    public void process() {
        try {
            //noinspection UnstableApiUsage
            List<String> cardList = Files.readLines(new File("C:\\temp\\errorUserType.txt"), StandardCharsets.UTF_8, new LineProcessor<List<String>>() {

                private final List<String> cardList = Lists.newArrayList();

                @Override
                public boolean processLine(String s) {
                    cardList.add(StringUtils.trim(s));
                    return true;
                }

                @Override
                public List<String> getResult() {
                    return cardList;
                }
            });
            Date start = LocalDate.parse("2020-03-20").toDate();
            Date end = LocalDate.parse("2020-04-11").toDate();
            for (String cardNo : cardList) {
                log.info("卡号：{}", cardNo);
                String eCardNo = cardNo.substring(4);
                CardInfo cardInfo = commonBaseDao.queryCardInfoByECardNo(eCardNo);
                if (cardInfo != null && cardInfo.getEcardStatus() != CardStaticValue.EcardStatus.EcardState_JY
                        && cardInfo.getECardType() == CardStaticValue.EcardType.CARD_TYPE_JZ &&
                        !StringUtils.equals(cardInfo.getSignSource(), SignSourceEnum.CENTER.getSource()) &&
                        cardInfo.getCardNetID() == 6101 &&
                        cardInfo.getOptOn().compareTo(start) > 0 && cardInfo.getOptOn().compareTo(end) < 0) {
                    String channelType = cardInfo.getChannelType();
                    if (!"YSF".equals(channelType) && !"CMB".equals(channelType) && !"CMBU".equals(channelType)
                            && !"BOCM".equals(channelType)) {
                        Integer count = countMap.get(channelType);
                        if (count == null) {
                            count = 1;
                        } else {
                            count++;
                        }
                        countMap.put(channelType, count);
                        log.info("满足条件");
                        errorCardList.add(cardNo);
                    } else {
                        log.info("不满足条件");
                    }
                } else {
                    log.info("不满足条件");
                }
            }
            log.info(countMap.toString());
            StringBuilder builder = new StringBuilder();
            for (String errorCard : errorCardList) {
                builder.append(errorCard).append("\n");
            }

            //noinspection UnstableApiUsage
            Files.write(builder.toString(), new File("C:\\temp\\errorCard.txt"), StandardCharsets.UTF_8);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
    }

}
